package com.zgw.mykotlin

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import java.util.*

class MainActivity : AppCompatActivity() {
    var maxSum: Int = 1;

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        println("Hello, Kotlin!")
//        val tvText = findViewById<TextView>(R.id.textView)
//        tvText.setText(max(10, 100))
//        val person = Person("Lucy", true);
//        person.isMarried = true;

        val rectangle = Rectangle(43, 41)

        println("---------:" + rectangle.isSquare)

        println("---------:" + Color.BLUE.rgb())

        println("---------:" + getMnemonic(Color.BLUE))

        println("---------:" + mix(Color.BLUE, Color.YELLOW))

        println("---------:" + mixOptimized(Color.BLUE, Color.YELLOW))

        println("---------:" + eval(Sum(Sum(Num(1), Num(2)), Num(4))))

        println("---------:" + evalWithLogging(Sum(Sum(Num(1), Num(2)), Num(4))))

        //闭区间：包括100 zgw 2018/8/14 0014 16:57:52
        for (i in 1..100) {
            println(fizzBuzz(i))
        }

        println("--------------------------")

        for (i in 100 downTo 1 step 2) {
            println(fizzBuzz(i))
        }
        
        //开区间：不包括100 zgw 2018/8/14 0014 16:57:37 
        for (i in 0 until 100) { 
            
        }

        converBinary()

    }

//    fun eval (expr: Expr): Int {
//        if (expr is Num) {
//            return expr.value //智能转换 zgw 2018/8/14 0014 15:34:06
//        }
//        if (expr is Sum) {
//            return eval(expr.left) + eval(expr.right)
//        }
//        throw IllegalArgumentException ("Unknow expression")
//    }

//    fun eval (expr: Expr): Int =
//            if (expr is Num) {
//                expr.value
//            } else if (expr is Sum) {
//                eval(expr.right) + eval(expr.left)
//            } else {
//                throw IllegalArgumentException ("Unknow expression")
//            }

    fun eval (expr: Expr): Int =
            when (expr) {
                is Num -> expr.value
                is Sum -> eval(expr.left) + eval(expr.right)
                else -> throw IllegalArgumentException ("Unknow expression")
            }

    fun evalWithLogging(expr: Expr) : Int =
        when(expr) {
            is Num -> {
                println("num: ${expr.value}")
                expr.value
            }
            is Sum -> {
                val left = evalWithLogging(expr.left)
                val right = evalWithLogging(expr.right)
                println("sum = $left + $right")
                left + right
            }
            else -> throw IllegalArgumentException ("Unknow expression")
        }

    fun max(a: Int, b: Int) : Int {
        return if (a > b) a else b;
    }

    fun max1(a: Int, b: Int) = if (a > b) a else b;

    fun getMnemonic(color: Color) =
            when (color) {
                Color.RED -> "Richard"
                Color.ORANGE -> "Of"
                Color.YELLOW -> "York"
                Color.GREEN -> "Gave"
                Color.BLUE -> "Battle"
                Color.INDIGO -> "In"
                Color.VIOLET -> "Vain"
            }

    fun mix(c1: Color, c2: Color) =
            when (setOf(c1, c2)) {
                setOf(Color.RED, Color.YELLOW) -> Color.ORANGE
                setOf(Color.YELLOW, Color.BLUE) -> Color.GREEN
                setOf(Color.BLUE, Color.VIOLET) -> Color.INDIGO

                else -> throw Exception("Dirty color")
            }

    fun mixOptimized(c1: Color, c2: Color) =
            when {
                (c1 == Color.RED && c2 == Color.YELLOW) || (c1 == Color.YELLOW && c2 == Color.RED) -> Color.ORANGE
                (c1 == Color.YELLOW && c2 == Color.BLUE) || (c1 == Color.BLUE && c2 == Color.YELLOW) -> Color.GREEN
                (c1 == Color.BLUE && c2 == Color.VIOLET) || (c1 == Color.VIOLET && c2 == Color.BLUE) -> Color.INDIGO

                else -> throw Exception("Dirty color")
            }

    fun fizzBuzz(i: Int) = when {
        i % 3 == 0 -> "Fizz "
        i % 5 == 0 -> "Buzz "
        i % 15 == 0 -> "FizzBuzz "
        else -> "$i "
    }

    fun converBinary() {
        val binaryReps = TreeMap<Char, String>()

        for (c in 'A'..'F') {
            val binary = Integer.toBinaryString(c.toInt())
            binaryReps[c] = binary
        }

        for ((letter, binary) in binaryReps) {
            println("$letter = $binary")
        }

        val list = arrayListOf("111", "222", "333" )
        for ((index, element) in list.withIndex()) {
            println("$index : $element")
        }
    }
}
